package com.atguign.gulimall.product.service.impl;

import com.atguign.gulimall.product.dao.SkuInfoDao;
import com.atguign.gulimall.product.entity.SkuInfoEntity;
import com.atguign.gulimall.product.feign.SeckillFeifnService;
import com.atguign.gulimall.product.service.*;
import com.atguign.gulimall.product.to.SeckillInfoVo;
import com.atguign.gulimall.product.vo.SkuItemVo;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.common.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;
    @Autowired
    SpuInfoDescService spuInfoDescService;
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    SeckillFeifnService seckillFeifnService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        LambdaQueryWrapper<SkuInfoEntity> spuInfoEntityQueryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.hasText(key)) {
            spuInfoEntityQueryWrapper.and((w) -> {
                w.eq(SkuInfoEntity::getSkuId, key)
                        .or()
                        .like(SkuInfoEntity::getSkuName, key);
            });
        }
        String brandId = (String) params.get("brandId");
        if (StringUtils.hasText(brandId)) {
            spuInfoEntityQueryWrapper.and((w) -> {
                w.eq(SkuInfoEntity::getBrandId, brandId);
            });
        }
        String min = (String) params.get("min");
        if (StringUtils.hasText(min) && !min.equals("0")) {
            spuInfoEntityQueryWrapper.and((w) -> {
                w.ge(SkuInfoEntity::getPrice, min);
            });
        }
        String max = (String) params.get("max");
        if (StringUtils.hasText(max) && !max.equals("0")) {
            spuInfoEntityQueryWrapper.and((w) -> {
                w.le(SkuInfoEntity::getPrice, max);
            });
        }
        String catelogId = (String) params.get("catelogId");
        if (StringUtils.hasText(catelogId)) {
            spuInfoEntityQueryWrapper.and((w) -> {
                w.eq(SkuInfoEntity::getCatalogId, catelogId);
            });
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                spuInfoEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();

        /**
         * 多线程
         */
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, executor);
        CompletableFuture<Void> despFuture = infoFuture.thenAcceptAsync(info -> {
            skuItemVo.setDesp(spuInfoDescService.getById(info.getSpuId()));
        }, executor);
        CompletableFuture<Void> aleAttrFuture = infoFuture.thenAcceptAsync(info -> {
            skuItemVo.setSaleAttr(skuSaleAttrValueService.getSaleAttrsBySpuId(info.getSpuId()));
        }, executor);
        CompletableFuture<Void> spuSaleAttrFuture = infoFuture.thenAcceptAsync(info -> {
            skuItemVo.setSpuSaleAttr(attrGroupService.getAttrGroupWithAttrsBySpuId(info.getSpuId(), info.getCatalogId()));
        }, executor);
        CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> {
            skuItemVo.setImages(skuImagesService.getImagesBySkuId(skuId));
        }, executor);
        CompletableFuture<Void> getSecondKillSkuInfo = CompletableFuture.runAsync(() -> {
            R seckillSkuInfo = seckillFeifnService.getSeckillSkuInfo(skuId);
            if (seckillSkuInfo.getCode() == 0) {
                SeckillInfoVo data = R.toPoJo(seckillSkuInfo.get("data"), SeckillInfoVo.class);
                if(data!=null)
                    skuItemVo.setSeckillInfoVo(data);
            }
        }, executor);
        //等待任务完成
        CompletableFuture.allOf(infoFuture, despFuture, aleAttrFuture, spuSaleAttrFuture, imagesFuture,getSecondKillSkuInfo).get();
        log.debug(skuItemVo.toString());
        return skuItemVo;
    }

}